//
//  HappyPlayCell.m
//  HappyBaby
//
//  Created by lanou on 15/11/24.
//  Copyright © 2015年 华哥. All rights reserved.
//

#import "HappyPlayCell.h"
#import "Define.h"
#import "HappyPlayCollectionCell.h"
#import "HappyModel.h"
@interface HappyPlayCell () <UICollectionViewDataSource,UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@end

static NSString *cellIdentifier = @"Cell";

@implementation HappyPlayCell
{
    CGFloat _scale;
    CGFloat X;
    CGFloat itemHeight;
    CGFloat itemWidth;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
    if (self) {
        
        _scale = kScreenHeight / 667;
        // 创建label
        X = 20;
        CGFloat width = kScreenWidth - 2 * X;
        self.postAuthor = [[UILabel alloc] initWithFrame:CGRectMake(X, 0, width, 50 * _scale)];
        self.postAuthor.font = [UIFont boldSystemFontOfSize:kScreenWidth/21];
        self.postAuthor.textColor = [UIColor darkGrayColor];
        [self.contentView addSubview:self.postAuthor];
        
        // 创建collectionView
        // 创建layOut
        itemHeight = ((width - 20)/3 + 60)  * kScaleX;
        itemWidth = (width - 20)/3 * kScaleY;
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(itemWidth, itemHeight);
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 10;
        
        //NSLog(@"layout: %@",layout);
        
        self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(X, self.postAuthor.height, width, itemHeight) collectionViewLayout:layout];
        self.collection.scrollEnabled = NO;
        
        // 设置代理
        self.collection.dataSource = self;
        self.collection.delegate = self;
        self.collection.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.collection];
        
       //NSLog(@"%@",self.collection);
        
        // 注册自定义cell
        [self.collection registerClass:[HappyPlayCollectionCell class] forCellWithReuseIdentifier:cellIdentifier];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.collection.y + itemHeight, kScreenWidth, 2)];
        lineView.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:lineView];
        
        // 查看更多Button
        self.checkMoreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.checkMoreBtn.frame = CGRectMake(X, lineView.y + lineView.height, kScreenWidth, 45 * _scale) ;
        [self.checkMoreBtn setTitle:@"查看更多>>" forState:UIControlStateNormal];
        [self.checkMoreBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        self.checkMoreBtn.titleLabel.font = [UIFont boldSystemFontOfSize:18];
        [self.contentView addSubview:self.checkMoreBtn];
        
     }
    return self;
}

#pragma mark --model的setModelArray 方法
- (void)setModelArray:(NSArray *)modelArray
{
    _modelArray = modelArray;
    [self.collection reloadData];
}


// 分区下返回Item的数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.modelArray.count;
}
// 返回行collectionView cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HappyPlayCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    
    cell.happyModel = self.modelArray[indexPath.item];
    NSLog(@"cell %ld = %p",indexPath.item, cell);
    return cell;
}

// 返回上下左右的边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 51 * _scale, 0);
}

#pragma mark --选中cell中Item跳转的方法  
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld",indexPath.item);
    HappyModel * model = self.modelArray[indexPath.item];
    [self.delegate pushToDetailViewControloler:model];
}

@end
